package com.lzb.newloan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.lzb.newloan.R;
import com.lzb.newloan.pojo.BorrowPersonPojo;
import com.lzb.newloan.pojo.InvestPojo;

import java.util.ArrayList;
import java.util.HashMap;

public class InvestRecordAdapter extends BaseAdapter {

	private ArrayList<InvestPojo> pojo;
	private LayoutInflater mInflater;
	private Context context;
	HashMap<Integer, View> localAppMap = new HashMap<Integer, View>();

	public InvestRecordAdapter(Context context, ArrayList<InvestPojo> pojo) {
		this.pojo = pojo;
		this.mInflater = LayoutInflater.from(context);
		this.context = context;
	}

	public ArrayList<InvestPojo> getPojo() {
		return pojo;
	}

	public void setPojo(ArrayList<InvestPojo> pojo) {
		this.pojo = pojo;
	}

	/**
	 */
	@Override
	public int getCount() {
		return pojo.size();
	}

	@Override
	public Object getItem(int arg0) {

		return pojo.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		ViewHoler viewHolder = null;
		if (localAppMap.get(position) == null) {
			viewHolder = new ViewHoler();
			convertView = mInflater.inflate(R.layout.adapter_invest_record, null);
			viewHolder.money_type = (TextView) convertView
					.findViewById(R.id.money_type);
			viewHolder.money_time = (TextView) convertView.findViewById(R.id.money_time);
			viewHolder.get_money_value = (TextView) convertView.findViewById(R.id.get_money_value);
			viewHolder.invest_detail = (LinearLayout) convertView.findViewById(R.id.invest_detail);
			viewHolder.person_detail_list = (ListView) convertView.findViewById(R.id.person_detail_list);
			viewHolder.look_detail = (TextView) convertView.findViewById(R.id.look_detail);
			localAppMap.put(position, convertView);
			convertView.setTag(viewHolder);
		} else {
			convertView = localAppMap.get(position);

			viewHolder = (ViewHoler) convertView.getTag();
		}
		final InvestPojo data = pojo.get(position);
        final ViewHoler holder = viewHolder;
        viewHolder.money_type.setText(data.getType());
        viewHolder.get_money_value.setText("￥"+data.getMoney());
		viewHolder.money_time.setText(data.getTime());
		viewHolder.invest_detail.setTag(data);
        viewHolder.invest_detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
				InvestPojo pojo1 = (InvestPojo) holder.invest_detail.getTag();

                if(data.isCheck()){
                    holder.person_detail_list.setVisibility(View.GONE);
					pojo1.setCheck(false);
					holder.look_detail.setText("查看详情");
                }else{
                    holder.person_detail_list.setVisibility(View.VISIBLE);
					holder.look_detail.setText("关闭详情");
                    ArrayList<BorrowPersonPojo> list = pojo1.getPojo();
                    InvestRecordPersonAdapter adapter = new InvestRecordPersonAdapter(context,list);
                    holder.person_detail_list.setAdapter(adapter);
					adapter.notifyDataSetChanged();
					setListViewHeight(holder.person_detail_list,adapter,list.size());
					pojo1.setCheck(true);
                }

				notifyDataSetChanged();
            }
        });

		return convertView;
	}

	static class ViewHoler {
		public TextView money_type,money_time,get_money_value,look_detail;
		public  LinearLayout invest_detail;
		public ListView person_detail_list;
	}

	static void setListViewHeight(ListView listView,
								  InvestRecordPersonAdapter adapter, int count) {
		int totalHeight = 0;
		for (int i = 0; i < count; i++) {
			View listItem = adapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}
		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight + (listView.getDividerHeight() * count);
		listView.setLayoutParams(params);
	}


}
